/**
 *
 * @author yuan_rz
 * @date 2019/03/11 14:28
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {

        },
        table_where = {

        },
        cols = [[
              {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left',hide : true}
            , {field: 'TASK_NAME', width: 150, title: '任务名', fixed: 'left', sort: true}
            , {field: 'TASK_STATUS', width: 100, title: '任务状态',filterDatas: app.datas_('sys_task_status')}
            , {field: 'TASK_GROUP', width: 100, title: '任务组',filterDatas: app.datas_('sys_task_group')}
            , {field: 'TASK_REMARK', width: 200, title: '说明'}
            , {
                width: 60,
                title: '',
                toolbar: '#script-tasksetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        taskSettingAction,
        moduleName = '定时任务',
        editArea = ['720px', '420px']
    ;

    var task = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'task',
                _searchs: [{
                    id: 'task', tableId: 'task_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'task_edit', tpl: 'system/task_edit'
                },{
                    dom: 'common_open_date', tpl: 'common/_open_date'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'task_table'),
                    url: g_ctxPath + 'sys/task/getListData',
                    exportsUrl: g_ctxPath + 'sys/task/exportListData',
                    exportsName: moduleName + '数据',
                    title: moduleName + '表',
                    id: 'task_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],     //, 'exports'
                    height: '300px',
                    cols: cols,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-tasksetting-action').innerHTML).render({}, function (string) {
                    taskSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(task_table)', function (obj) {
                    switch(obj.event) {
                        case 'detail':
                            layer.tips(taskSettingAction, obj.td, {
                                tips: [3, '']
                                , time: 5000
                                , anim: 500
                                , maxWidth: 120
                                , skin: 'layui-table-tips'
                                , success: function (layero, index) {
                                    tipsIndex = index;
                                    layero.find('li').on('click', function () {
                                        switch ($(this).attr('inx')) {
                                            case '1':
                                                if(obj.data.TASK_STATUS == '1'){
                                                    app.showWarning('已启动状态不可编辑，请先停止任务');
                                                }else{
                                                    self._openTplFull({
                                                        tag: 'task_edit',
                                                        moduleName: moduleName,
                                                        tagData: {
                                                            obj : obj
                                                        },
                                                        index: 0,
                                                        openOpts: {
                                                            title: moduleName + '编辑',
                                                            area: editArea
                                                        }
                                                    });
                                                }
                                                break;
                                            case '2':
                                                if(obj.data.TASK_STATUS == '1'){
                                                    app.showWarning('已启动状态不可删除，请先停止任务');
                                                }else{
                                                    self.deleteData(obj);
                                                }
                                                break;
                                            case '3':
                                                if(obj.data.TASK_STATUS == '1'){
                                                    app.showWarning('已启动状态不可重复启动');
                                                }else{
                                                    self.runTask(obj);
                                                }
                                                break;
                                            case '4':
                                                if(obj.data.TASK_STATUS == '0'){
                                                    app.showWarning('未启动状态不可重复停止');
                                                }else{
                                                    self.stopTask(obj);
                                                }
                                                break;
                                            case '5':
                                                if(obj.data.TASK_GROUP == '1'){
                                                    self._openTplFull({
                                                        tag: 'common_open_date',
                                                        moduleName : moduleName,
                                                        tagData: {
                                                            label: '同步日期',
                                                            cb : function(date){
                                                                obj.data.trandate = date;
                                                                self.runTaskOnce(obj);
                                                            }
                                                        },
                                                        index: 1,
                                                        openOpts: {
                                                            title:'选择同步日期',
                                                            area: ['400px','200px']
                                                        }
                                                    });
                                                }else{
                                                    app.confirm({
                                                        msg: '是否执行一次选择的任务？',
                                                        ok: function () {
                                                            self.runTaskOnce(obj);
                                                        }
                                                    });
                                                }
                                                break;
                                        }
                                        layer.close(index);
                                        tipsIndex = null;
                                    });
                                }
                            });
                            return false;
                            break;
                    }
                });

                //监听行双击事件
                table.on('rowDouble(task_table)', function (obj) {
                    self._openTplFull({
                        tag: 'task_edit',
                        moduleName : moduleName,
                        tagData: {
                            obj : obj
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '编辑',
                            area: editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(task_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'task_edit',
                                moduleName : moduleName,
                                tagData: {
                                    is_add : true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '编辑',
                                    area: editArea
                                }
                            });
                            break;
                    }
                });
            });
        },
        reloadTable: function () {
            self._reloadTable({id: 'task_table', where: table_where});
        },
        deleteData: function (obj) {
            app.ajax({
                xurl: 'sys/task/deleteData',
                xdata: obj.data,
                confirm: true,
                loading: true,
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTable();
                    }
                }
            });
        },
        runTask: function (obj) {
            app.ajax({
                xurl: 'sys/task/runTask',
                xdata: obj.data,
                confirm: {
                    msg: '是否启动选择的任务?'
                },
                loading: true,
                okmsg: moduleName +'启动成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTable();
                    }
                }
            });
        },
        stopTask: function (obj) {
            app.ajax({
                xurl: 'sys/task/stopTask',
                xdata: obj.data,
                confirm: {
                    msg: '是否停止选择的任务?'
                },
                loading: true,
                okmsg: moduleName +'停止成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTable();
                    }
                }
            });
        },
        runTaskOnce: function (obj) {
            app.ajax({
                xurl: 'sys/task/runTaskOnce',
                xdata: obj.data,
                loading: true,
                okmsg: moduleName +'已于后台执行',
                cb: function (result) {
                    if (result.ok) {

                    }
                }
            });
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'task',
                    methods: {

                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new task();

});